package io.renren.modules.job.task;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.lc.ibps.base.core.encrypt.EncryptUtil;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.StrFormatter;
import io.renren.modules.app.entity.ibps.Employee;
import io.renren.modules.app.entity.ibps.PartyEntity;
import io.renren.modules.app.entity.ibps.UserParty;
import io.renren.modules.app.entity.ibps.UserRole;
import io.renren.modules.app.service.IEmployeeService;
import io.renren.modules.app.service.IPartyEntityService;
import io.renren.modules.app.service.IUserPartyService;
import io.renren.modules.app.service.IUserRoleService;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import net.duguying.pinyin.Pinyin;
import net.duguying.pinyin.PinyinException;

/**
 * 家校通用户同步
 * 
 * @author Administrator
 *
 */
@Slf4j
@Component
public class JxtSyncTask {

	@Autowired
	private IEmployeeService employeeService;
	@Autowired
	private IUserRoleService userRole;
	@Autowired
	private IUserPartyService userService;
	@Autowired
	private IPartyEntityService partyEntityService;

	@Autowired
	@Qualifier("jiaxiaotongJdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Synchronized
	@Scheduled(fixedDelay = 1000 * 60 * 15)
	public void sync() {
//		List<Employee> findAll = employeeService.findAll();
//		findAll.forEach(e -> {
//			System.out.println(e);
//		});
//		System.out.println("教师-------------");
//		List<Teacher> findMy = teacherService.findMy();
//		findMy.forEach(f->{
//			System.out.println("教师:"+f.getUserName());
//		});
		List<Map<String, Object>> queryForList = this.jdbcTemplate.queryForList("SELECT * FROM db_teacher");
		queryForList.forEach(data -> {
			System.out.println(data);
			Employee employee = new Employee();
			employee.setName(data.get("name").toString());
			// sex 0男,1女
			employee.setGender(Convert.toInt(data.get("sex")) == 1 ? "male" : "female");
			employee.setMobile(data.get("telphone").toString());
			Object openId = data.get("open_id");
			if (openId != null) {
				employee.setWcAccount(Convert.toStr(openId));
			}

			employee.setCreateTime(new Date(System.currentTimeMillis()));
			List<Employee> findByMobile = employeeService.findByMobile(employee.getMobile());
			if (findByMobile.size() > 0) {
				log.info("存在:" + employee.getName());
				return;
			}
			if(employee.getWcAccount() == null || "".equals(employee.getWcAccount())) {
				log.info("教师:{}没有绑定微信,不执行!!",employee.getName());
				return ;
			}
			employee.setStatus("actived");
			log.info("保存前:" + employee);
			Employee emSave = employeeService.save(employee);
			log.info("保存:" + emSave);
			String namePinyin = null;
			try {
				namePinyin = new Pinyin().translateNoMark(employee.getName());
			} catch (PinyinException e2) {
				e2.printStackTrace();
				return;
			}
			String password = EncryptUtil.encryptSha256("1");
			// 保存登陆user
			UserParty userParty = new UserParty();
			userParty.setAccount(namePinyin);
			userParty.setPassword(password);
			userParty.setIs_super("N");
			userParty.setId(emSave.getId());
			userParty.setCREATE_TIME_(new Date(System.currentTimeMillis()));

			String format = StrFormatter.format("{}{}{}", namePinyin, password, "N");
			String dataCheck = EncryptUtil.encryptSha256(format);
			userParty.setDATA_CHECK_(dataCheck);
			userService.save(userParty);

			// 插入 partyEntityService
			PartyEntity partyEntity = new PartyEntity();
			partyEntity.setParty_type_("employee");
			partyEntity.setId(emSave.getId());
			partyEntity.setPath_(emSave.getId() + ".");
			partyEntity.setParty_alias_(namePinyin);
			partyEntity.setName_(employee.getName());
			partyEntity.setParent_id_("0");
			partyEntity.setDepth_("1");
			partyEntity.setCreate_by_("1");
			partyEntity.setCREATE_TIME_(new Date(System.currentTimeMillis()));
			partyEntityService.save(partyEntity);

			// 协和小学教师id
			Long roleIdTeacher = 567364303959621632l;
			UserRole ur = new UserRole();
			ur.setUserId(emSave.getId());
			ur.setRoleId(roleIdTeacher);
			ur.setCreateTime(new Date(System.currentTimeMillis()));
			userRole.save(ur);
			log.info("保存完成教师:" + employee);
		});
	}
}
